package team.seekers.skill.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import team.seekers.skill.common.enums.RoleEnum;
import team.seekers.skill.service.UserService;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * @author Godc
 * @description
 */
public class SkillUtils {

    @Autowired
    private UserService userService;

    //获得当前登录用户的userId
    public static String getUserId(){
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userInfo.getUserId();
    }

    public static RoleEnum getRole(){
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userInfo.getRole();
    }


    //MD5加密
    public static String md5(String source) {
        if (source == null || source.length() == 0) {
            throw new RuntimeException("账号不能为空，请重新输入");
        }
        try {
            String algorithm = "md5";
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            // 获取铭文字符串对应的子节数组
            byte[] input = source.getBytes();
            // 执行加密
            byte[] output = messageDigest.digest(input);
            // 创建BigInteger对象
            int signum = 1;
            final BigInteger bigInteger = new BigInteger(signum, output);

            // 按照16进制将bigInteger的值转换为字符串
            int radix = 16;
            String encoded = bigInteger.toString(radix);
            return encoded;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    //获取两个日期间的天数差
    public static float differentDaysByMillisecond(Date date1, Date date2) {
        System.out.println(date1+"        "+date2);
        long diffSeconds = date2.getTime() - date1.getTime();
        long diffHour = diffSeconds / (1000 * 3600);
        float diffDay = (float) diffHour / 24;
        float floatDay = decimalFormat("0.00", diffDay);
        return floatDay;
    }
    //日期格式转化
    public static float decimalFormat(String pattern, double value) {
        return Float.parseFloat(new DecimalFormat(pattern).format(value));
    }
    //判断是不是那五种类型
    public static boolean isTrueType(String type){
        if (type.equals("computer") || type.equals("code") ||type.equals("life") || type.equals("beauty") || type.equals("money"))
            return true;
        else
            return false;
    }
}
